package org.elasticsearch.indices.analysis;

import org.apache.lucene.analysis.Tokenizer;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment;
import org.elasticsearch.index.analysis.*;
import org.lionsoul.jcseg.analyzer.JcsegAnalyzer;
import org.lionsoul.jcseg.analyzer.JcsegTokenizer;
import org.lionsoul.jcseg.core.ADictionary;
import org.lionsoul.jcseg.core.DictionaryFactory;
import org.lionsoul.jcseg.core.JcsegTaskConfig;

import java.io.File;

/**
 * Registers indices level analysis components so, if not explicitly configured,
 * will be shared among all indices.
 */
public class JcsegIndicesAnalysis extends AbstractComponent {
    private static final ESLogger log = Loggers.getLogger("jcseg-analyzer");
    @Inject
    public JcsegIndicesAnalysis(final Settings settings,
                                IndicesAnalysisService indicesAnalysisService, Environment env) {
        super(settings);
        log.info("JcsegIndicesAnalysis initializing ...");
        File proFile = new File(env.configFile()+"/jcseg/" + JcsegTaskConfig.LEX_CFG_FILE);
        int mode = JcsegTaskConfig.COMPLEX_MODE;
        indicesAnalysisService.analyzerProviderFactories().put("jcseg",
                new PreBuiltAnalyzerProviderFactory("jcseg", AnalyzerScope.INDICES,
                        proFile.exists() ? new JcsegAnalyzer(mode, proFile.getPath()) : new JcsegAnalyzer(mode)));
        mode = JcsegTaskConfig.DETECT_MODE;
        indicesAnalysisService.analyzerProviderFactories().put("jcseg_detect",
                new PreBuiltAnalyzerProviderFactory("jcseg_detect", AnalyzerScope.INDICES,
                        proFile.exists() ? new JcsegAnalyzer(mode, proFile.getPath()) : new JcsegAnalyzer(mode)));
        mode = JcsegTaskConfig.SIMPLE_MODE;
        indicesAnalysisService.analyzerProviderFactories().put("jcseg_simple",
                new PreBuiltAnalyzerProviderFactory("jcseg_simple", AnalyzerScope.INDICES,
                        proFile.exists() ? new JcsegAnalyzer(mode, proFile.getPath()) : new JcsegAnalyzer(mode)));

        ADictionary dic;
        JcsegTaskConfig config;
        config = new JcsegTaskConfig(proFile.exists() ? proFile.getPath() : null);
        dic = DictionaryFactory.createDefaultDictionary(config);
        indicesAnalysisService.tokenizerFactories().put("jcseg",
                new PreBuiltTokenizerFactoryFactory(new TokenizerFactory() {
                    @Override
                    public String name() {
                        return "jcseg";
                    }

                    @Override
                    public Tokenizer create() {
                        try {
                            int mode = JcsegTaskConfig.COMPLEX_MODE;
                            return new JcsegTokenizer(mode, config, dic);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        return null;
                    }
                }));
        indicesAnalysisService.tokenizerFactories().put("jcseg_detect",
                new PreBuiltTokenizerFactoryFactory(new TokenizerFactory() {
                    @Override
                    public String name() {
                        return "jcseg_detect";
                    }

                    @Override
                    public Tokenizer create() {
                        try {
                            int mode = JcsegTaskConfig.DETECT_MODE;
                            return new JcsegTokenizer(mode, config, dic);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        return null;
                    }
                }));

        indicesAnalysisService.tokenizerFactories().put("jcseg_simple",
                new PreBuiltTokenizerFactoryFactory(new TokenizerFactory() {
                    @Override
                    public String name() {
                        return "jcseg_simple";
                    }

                    @Override
                    public Tokenizer create() {
                        try {
                            int mode = JcsegTaskConfig.SIMPLE_MODE;
                            return new JcsegTokenizer(mode, config, dic);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        return null;
                    }
                }));
        log.info("JcsegIndicesAnalysis initialized");
    }
}